//创建舞台
var stage = new createjs.Stage("gameView");
createjs.Ticker.setFPS(30);
createjs.Ticker.addEventListener("tick",stage);

var gameView = new createjs.Container();
gameView.x = 60;
gameView.y = 155;
stage.addChild(gameView);

var circleArr = [[],[],[],[],[],[],[],[],[]];
var currentCat;
var catSprite;
var MOVE_NONE=-1,MOVE_LEFT=0,MOVE_UP_LEFT=1,MOVE_UP_RIGHT=2,MOVE_RIGHT=3,MOVE_DOWN_RIGHT=4,MOVE_DOWN_LEFT=5;


//判断猫的方向
function getMoveDir(cat){

	var distanceMap=[];
	//left
	var can = true;
	for(var x=cat.indexX;x>=0;x--){
		if(circleArr[x][cat.indexY].getCircleType()==circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_LEFT] = cat.indexX-x;
			break;
		}
	}
	if(can){
		return MOVE_LEFT;
	}

	//left up
	can = true;
	var x = cat.indexX,y = cat.indexY;
	while(true){
		if(circleArr[x][y].getCircleType() == circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_UP_LEFT] = cat.indexY-y;
			break;
		}
		if(y%2 == 0){
			x--;
		}
		y--;
		if(x<0||y<0){
			break;
		}
	}
	if(can){
		return MOVE_UP_LEFT;
	}

	//right up
	can = true;
	var x = cat.indexX,y = cat.indexY;
	while(true){
		if(circleArr[x][y].getCircleType() == circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_UP_RIGHT] = cat.indexY-y;
			break;
		}
		if(y%2 == 1){
			x++;
		}
		y--;
		if(x>8||y<0){
			break;
		}
	}
	if(can){
		return MOVE_UP_RIGHT;
	}

	//right
	can = true;
	for(var x = cat.indexX;x<9;x++){
		if(circleArr[x][cat.indexY].getCircleType() == circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_RIGHT] = x-cat.indexX;
			break;
		}
	}
	if(can){
		return MOVE_RIGHT;
	}

	//right down
	can = true;
	var x = cat.indexX,y = cat.indexY;
	while(true){
		if(circleArr[x][y].getCircleType() == circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_DOWN_RIGHT] = y-cat.indexY;
			break;
		}
		if(y%2 == 1){
			x++;
		}
		y++;
		if(y>8||x>8){
			break;
		}
	}
	if(can){
		return MOVE_DOWN_RIGHT;
	}

	//left down
	can = true;
	var x = cat.indexX,y = cat.indexY;
	while(true){
		if(circleArr[x][y].getCircleType() == circle.TYPE_SELECTED){
			can = false;
			distanceMap[MOVE_DOWN_LEFT] = y-cat.indexY;
			break;
		}
		if(y%2 == 0){
			x--;
		}
		y++;
		if(y>8||x<0){
			break;
		}
	}
	if(can){
		return MOVE_DOWN_LEFT;
	}

	//判断已走过的方向是否还有步可走
	var maxDir = -1,maxValue = -1;
	for(var dir=0;dir<distanceMap.length;dir++){
		if(distanceMap[dir]>maxValue){
			maxValue = distanceMap[dir];
			maxDir = dir;
		}
	}
	if(maxValue>1){
		return maxDir;
	}else{
		return MOVE_NONE;
	}
}


function circleClicked(event){
	//判断圆圈是否被点击过
	if(event.target.pressed){
		return;
	}
	//判断当前点击圆圈是为是猫
	if(event.target.getCircleType()!=circle.TYPE_CAT){
		event.target.setCircleType(circle.TYPE_SELECTED);
	}else{
		return;
	}
	//判断当前猫是否在边界
	if(currentCat.indexX==0||currentCat.indexX==8||currentCat.indexY==0||currentCat.indexY==8){
		alert("你输了！");
		return;
	}
	//当前点击圆圈标记为已被点击过
	event.target.pressed = true;

	//判断猫走的方向
	var dir = getMoveDir(currentCat);
	switch (dir){
		case MOVE_LEFT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexX-1][currentCat.indexY];
			currentCat.setCircleType(circle.TYPE_CAT);
			// 更新图片猫操作
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		case MOVE_UP_LEFT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexY%2?currentCat.indexX:currentCat.indexX-1][currentCat.indexY-1];
			currentCat.setCircleType(circle.TYPE_CAT);
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		case MOVE_UP_RIGHT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexY%2?currentCat.indexX+1:currentCat.indexX][currentCat.indexY-1];
			currentCat.setCircleType(circle.TYPE_CAT);
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		case MOVE_RIGHT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexX+1][currentCat.indexY];
			currentCat.setCircleType(circle.TYPE_CAT);
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		case MOVE_DOWN_RIGHT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexY%2?currentCat.indexX+1:currentCat.indexX][currentCat.indexY+1];
			currentCat.setCircleType(circle.TYPE_CAT);
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		case MOVE_DOWN_LEFT:
			currentCat.setCircleType(circle.TYPE_UNSELECTED);
			currentCat = circleArr[currentCat.indexY%2?currentCat.indexX:currentCat.indexX-1][currentCat.indexY+1];
			currentCat.setCircleType(circle.TYPE_CAT);
			catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
			break;
		default:
			alert("你赢了！");
	}
}



function addCircles(){
	for(var indexY=0;indexY<9;indexY++){
		for(var indexX=0;indexX<9;indexX++){
			var c = new circle();
			gameView.addChild(c);
			circleArr[indexX][indexY] = c;
			c.indexX = indexX;
            c.indexY = indexY;
			c.x = indexY%2?indexX*65+30:indexX*65;
			c.y = indexY*65;
			//猫的初始位置
			if(indexX==4&&indexY==4){
				c.setCircleType(3);
				currentCat = c;
			}else if(Math.random()<0.1){
				c.setCircleType(circle.TYPE_SELECTED);
				c.pressed = true;
			}
			c.addEventListener("click",circleClicked);
		}
	}

	//添加神经猫图片
	var spriteSheet = new createjs.SpriteSheet({
		"images":["./images/catSprite.png"],
		"frames":{
			"height":180,
			"width":120,
			"count":2},
		"animations":{
			"run":[0,1]
		}
	});
	catSprite = new createjs.Sprite(spriteSheet,"run");
	// 添加图片猫到容器中
	gameView.addChild(catSprite);
	createjs.Ticker.setFPS(3);
	// 绘制到圆圈猫对象的位置，减去一定系数调整位置
	catSprite.setTransform(currentCat.x - 55, currentCat.y - 145,0.9,0.9);
}
addCircles();